package top.shiyiri.threading2;

/**
 * 使用同步方法解决实现 Runnable 接口的线程安全问题
 *
 *
 * @author Aunean
 * @date 2021/12/25
 */
class Window3 implements Runnable {

    private int ticket = 100;

    @Override
    public void run() {
        while (ticket > 0) {
            show();
        }
    }

    private synchronized void show() {
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + ":" + ticket);
            ticket--;
        }
    }
}

public class WindowTest3 {
    public static void main(String[] args) {

        Window3 w1 = new Window3();

        Thread t1 = new Thread(w1);
        t1.setName("窗口1");
        Thread t2 = new Thread(w1);
        t2.setName("窗口2");
        Thread t3 = new Thread(w1);
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
